//package com.imooc.bilibili.service;
//
//import com.imooc.bilibili.dao.repository.UserInfoRepository;
//import com.imooc.bilibili.dao.repository.VideoRepository;
//import com.imooc.bilibili.domain.UserInfo;
//import com.imooc.bilibili.domain.video.Video;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.common.text.Text;
//import org.elasticsearch.core.TimeValue;
//import org.elasticsearch.index.query.MultiMatchQueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
//import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.TimeUnit;
//
//@Service
//public class ElasticSearchService {
//
//
//    @Autowired
//    private VideoRepository videoRepository;
//
//    @Autowired
//    private UserInfoRepository userInfoRepository;
//
//    @Autowired
//    private RestHighLevelClient restHighLevelClient;
//
//
//    public void addUserInfo(UserInfo userInfo) {
//        userInfoRepository.save(userInfo);
//    }
//
//
//    public List<Map<String, Object>> getContents(String keyword,
//                                                 Integer pageNum,
//                                                 Integer pageSize) throws IOException {
//        String[] indices = {"videos", "user-infos"};
//        SearchRequest searchRequest = new SearchRequest(indices);
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        //分页
//        searchSourceBuilder.from(pageNum - 1);
//        searchSourceBuilder.size(pageSize);
//        MultiMatchQueryBuilder multiMatchQueryBuilder =
//                QueryBuilders.multiMatchQuery(keyword, "title", "nick", "description");
//        searchSourceBuilder.query(multiMatchQueryBuilder);
//        searchRequest.source(searchSourceBuilder);
//        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
//
//        //高亮显示
//        String[] arr = {"title", "nick", "description"};
//        HighlightBuilder highlightBuilder = new HighlightBuilder();
//        for (String key : arr) {
//            highlightBuilder.fields().add(new HighlightBuilder.Field(key));
//        }
//        highlightBuilder.requireFieldMatch(false); //如果要多个字段高亮。这里要设置为false
//        highlightBuilder.preTags("<span style=\"color:red\">");
//        highlightBuilder.postTags("</span>");
//        searchSourceBuilder.highlighter(highlightBuilder);
//
//        //开始搜索
//        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//        List<Map<String, Object>> list = new ArrayList<>();
//        for (SearchHit hit : searchResponse.getHits()) {
//            //处理高亮字段
//            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
//            Map<String, Object> sourceMap = hit.getSourceAsMap();
//            for (String key : arr) {
//                HighlightField field = highlightFields.get(key);
//                if (field != null) {
//                    Text[] fragments = field.fragments();
//                    String str = Arrays.toString(fragments);
//                    str = str.substring(1, str.length() - 1);
//                    sourceMap.put(key, str);
//                }
//            }
//            list.add(sourceMap);
//        }
//        return list;
//    }
//
//
//    public void addVideo(Video video) {
//        videoRepository.save(video);
//    }
//
//    public Video getVideos(String keyword) {
//        return videoRepository.findByTitleLike(keyword);
//    }
//
//
//    public void deleteAllVideos() {
//        videoRepository.deleteAll();
//    }
//}
